package com.desire.controller.sys;

import com.desire.bean.common.PageBean;
import com.desire.bean.common.ResultBean;
import com.desire.bean.sys.User;
import com.desire.common.validation.group.Login;
import com.desire.common.validation.group.Save;
import com.desire.controller.common.BaseController;
import com.desire.mapper.sys.UserMapper;
import com.desire.service.sys.UserService;
import com.desire.utils.TokenUtils;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author LittleDesire
 * @description
 * @date 2022-05-06 周五 16:15
 * @since 17
 */
@RestController
@RequestMapping("/api/user")
@Api(tags = "用户管理")
@Slf4j
public class UserController extends BaseController {
    @Autowired
    UserService userService;
    @Autowired(required = false)
    TokenUtils tokenUtils;

    @PostMapping("/query")
    @ApiOperation(value = "查询用户", notes = "查询用户数据，进行分页处理")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "pageNum", value = "当前第几页，默认第 1 页", dataType = "java.lang.Integer", paramType = "body"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示几条数据，默认为 10 条数据", dataType = "java.lang.Integer", paramType = "body")
    })
    @ApiResponses(value = {
            @ApiResponse(response = ResultBean.class, code = 200, message = "查询成功")
    })
    public ResultBean query(@ApiIgnore @RequestBody Map map) {
        PageBean pageBean = userService.query(map);
        return success(pageBean);
    }


    @PostMapping("/save")
    @ApiOperation(value = "新增用户", notes = "添加用户，会返回msg")
    public ResultBean save(@RequestBody @Validated(Save.class)  User user) {
        int rs = userService.save(user);
        return rs > 0 ? success("保存成功") : success("保存失败");
    }

//    @PostMapping("/login")
//    @ApiOperation(value = "用户登录", notes = "用户进行登录验证")
//    public ResultBean login(@RequestBody @Validated(Login.class)  User user) {
//        HashMap<Object, Object> map = new HashMap<>();
//        map.put("username", "root");
//        // String s = tokenUtils.generateToken(map);
//        return success("登陆成功", "s");
//    }

    @GetMapping("/login")
    public ResultBean login(){
        Map map = new HashMap();
        map.put("username","kk");
        map.put("password","987456");
        String token = tokenUtils.generateToken(map);
        return success("登录成功",token);
    }
}
